<template>
  <view class="container">
    <view class="example">
      <uni-forms ref="form" :model="driver" labelWidth="80px">
        <uni-forms-item label="姓名" name="driverName">
          <uni-easyinput v-model="driver.driverName" placeholder="请输入昵称" />
        </uni-forms-item>
        <uni-forms-item label="手机号码" name="driverPhone">
          <uni-easyinput v-model="driver.driverPhone" placeholder="请输入手机号码" />
        </uni-forms-item>
        <uni-forms-item label="年龄" name="driverAge">
          <uni-easyinput v-model="driver.driverAge" placeholder="请输入邮箱" />
        </uni-forms-item>
        <uni-forms-item label="驾龄" name="driverYears">
          <uni-easyinput v-model="driver.driverYears" placeholder="请输入驾龄" />
        </uni-forms-item>
		<uni-forms-item label="身份证" name="driverLicence">
		  <uni-button @click="chooseImage">选择图片</uni-button>
		</uni-forms-item>
		<uni-forms-item label="身份背景" name="driverBackground">
		  <uni-easyinput v-model="driver.driverBackground"/>
		</uni-forms-item>
      </uni-forms>
      <button type="primary" @click="submit">提交</button>
    </view>
  </view>
</template>

<script>
  import { getDriverInfo, updateDriverInfo } from "@/api/system/user"
  var driverId = localStorage.getItem("driverId");
  var token = localStorage.getItem("App-Token");
  export default {
    data() {
      return {
		driver: {
		  driverId:driverId,
		  driverName:'',
		  driverPhone:'',
		  driverAge:'',
	  	  driverYears:'',
		  driverLicence:'',
		  driverBackground:'',
		},
	  }
    },
    onLoad() {
      this.getDriver()
    },
    methods: {
      getDriver() {
        getDriverInfo(driverId).then(response => {
          this.driver = response.data
        })
      },
      submit(ref) {
        this.$refs.form.validate().then(res => {
          updateDriverInfo(this.driver).then(response => {
		   console.log(this.driver)
		   this.$modal.msgSuccess("修改成功")
		   this.$tab.reLaunch('/pages/index')
          })
        })
      },
	  chooseImage() {
        uni.chooseImage({
            count:1,
            success:(res)=>{
              const tempFilePaths = res.tempFilePaths;
              uni.uploadFile({
                url:'http://localhost:9002/user/driver/upload',		//post请求的地址
                filePath:tempFilePaths[0],
                success: (uploadFileRes) => {
					
                  //这里要注意，uploadFileRes.data是个String类型，要转对象的话需要JSON.parse一下
                  var data=JSON.parse(uploadFileRes.data);
				  console.log(data)
				  console.log(data.msg)
				  this.driver.driverLicence=data.msg;
				  console.log(this.driver.driverLicence)
				  alert("上传成功")
				  console.log(this.driver)
                }
              })
            } 
		})
	  }
   }
}
</script>

<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .example {
    padding: 15px;
    background-color: #fff;
  }

  .segmented-control {
    margin-bottom: 15px;
  }

  .button-group {
    margin-top: 15px;
    display: flex;
    justify-content: space-around;
  }

  .form-item {
    display: flex;
    align-items: center;
    flex: 1;
  }

  .button {
    display: flex;
    align-items: center;
    height: 35px;
    line-height: 35px;
    margin-left: 10px;
  }
</style>
